1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.eventbus.outside;
18
19 import com.google.common.eventbus.EventBus;
20 import com.google.common.eventbus.Subscribe;
21
22 import junit.framework.TestCase;
23
24 import java.util.concurrent.atomic.AtomicInteger;
25 import java.util.concurrent.atomic.AtomicReference;
26
27
28
29
30
31
32 public class OutsideEventBusTest extends TestCase {
33
34
35
36
37
38
39 public void testAnonymous() {
40 final AtomicReference<String> holder = new AtomicReference<String>();
41 final AtomicInteger deliveries = new AtomicInteger();
42 EventBus bus = new EventBus();
43 bus.register(new Object() {
44 @Subscribe
45 public void accept(String str) {
46 holder.set(str);
47 deliveries.incrementAndGet();
48 }
49 });
50
51 String EVENT = "Hello!";
52 bus.post(EVENT);
53
54 assertEquals("Only one event should be delivered.", 1, deliveries.get());
55 assertEquals("Correct string should be delivered.", EVENT, holder.get());
56 }
57 }